package it.amicosmanettone.finance.stocky.util;

import it.amicosmanettone.finance.yahoo.jsonbean.ResultSet;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.gson.Gson;

import android.util.Log;

public class SendData extends Thread {

	// http://finance.yahoo.com/d/quotes.csv?s=ACN,SSL.MI,MS.MI&f=snl1d1t1cv&e=.csv
	// http://www.jarloo.com/yahoo_finance/ SIMBOLI PARAMETRO f (x ritorna il
	// market di appartenenza da cui poi in teoria posso capire la moneta)

	// Ricerca simboli azioni
	// http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=ACN&callback=YAHOO.Finance.SymbolSuggest.ssCallback

	String baseUrl;
	static String YAHOO_FINANCE_SEARCH_API = "http://d.yimg.com/autoc.finance.yahoo.com/autoc?callback=YAHOO.Finance.SymbolSuggest.ssCallback&query=";
	private static String GOOGLE_FINANCE_QUOTE_API = "http://finance.google.com/finance/info?client=ig&q=";
	private static String GOOGLE_FINANCE_CHANGE_API = "http://rate-exchange.appspot.com/currency?";

	public static ResultSet searchStocksOnline(String query) {
		Log.v("SENDDATA", "SearchResults for : " + query);
		String response = "";
		ResultSet results = new ResultSet();

		try {

			HttpClientThread thread = new HttpClientThread(
					SendData.YAHOO_FINANCE_SEARCH_API, query);
			thread.start();
			synchronized (thread) {
				thread.wait();
			}
			response = HttpClientThread.threadOutput;
			try {

				String intestazione = "YAHOO.Finance.SymbolSuggest.ssCallback(";

				response = response.substring(intestazione.length());// Elimino
				// l'intestazione
				// dalla
				// risposta
				// del
				// server
				Log.v("SENDDATA",
						"SearchResults response senza intestazione : "
								+ response);
				response = response.substring(0, response.length() - 1);// Elimino
				// la
				// parentesi
				// tonda
				// alla
				// fine...
				Log.v("SENDDATA",
						"SearchResults response senza intestazione e tonda alla fine: "
								+ response);
				Gson gson = new Gson();

				String test = gson.toJson(results);
				results = gson.fromJson(response, ResultSet.class);
				Log.v("SENDDATA", ">>>>>>>>>>: "
						+ results.getResultSet().getResult().size());
			
			} catch (Exception e) {
				e.printStackTrace();

			}// End try...catch...

		}// End try...catch...

		catch (Exception e) {
			e.printStackTrace();
		}
		return (results);
		// if (SendData.results != null) {
		// Log.v("SENDDATA", "searchResult not null");
		// Log.v("SENDDATA", "searchResult contiene: "
		// + SendData.results.getResult().size() + " risultati");
		// } else {
		// Log.v("SENDDATA",
		// "searchResult is null: " + SendData.results.getResult());
		// }

	}// End method...

	

	
	public static double retrieveQuoteNuovo(String query) {
		String prova="";
		String resp="";
		try {
		HttpClientThread thread = new HttpClientThread(
				SendData.GOOGLE_FINANCE_QUOTE_API, query);
		thread.start();
		synchronized (thread) {
			thread.wait();
		}
		resp = HttpClientThread.threadOutput;
		
		
		
		
		
		
		
		
		
		
		
		
	
		String line = "";

		
			
			Log.v("WIDGET", "resp: " + resp);
			int position = resp.indexOf("l_cur") + 9;
			prova = resp.substring(position);
			Log.v("WIDGET", "prova: " + prova);
			int positionEnd = resp.indexOf("\"");
			prova = prova.substring(1, positionEnd + 1);
			Log.v("WIDGET", "SUBSTRING quote: " + prova);
		} catch (Exception e) {
			Log.w("WIDGET", e.getCause());
			e.printStackTrace();
		}// End try...catch...
		return Double.valueOf(prova);
	}// End method...
	
	
	
	
	public static double retrieveChangeCurrency(String curr1, String curr2) {

		String currency = "from=" + curr1 + "&to=" + curr2;
		String prova="";
		String resp="";
		try {
		HttpClientThread thread = new HttpClientThread(
				SendData.GOOGLE_FINANCE_CHANGE_API, currency);
		thread.start();
		synchronized (thread) {
			thread.wait();
		}
		resp = HttpClientThread.threadOutput;
		
		
	
			int position = resp.indexOf("rate") + 6;
			prova = resp.substring(position);
			int positionEnd = resp.indexOf(",");
			prova = prova.substring(1, positionEnd);
			Log.v("WIDGET", "SUBSTRING RATE: " + prova);
		} catch (Exception e) {
			Log.w("WIDGET", e.getCause());
			e.printStackTrace();
		}// End try...catch...
		return Double.valueOf(prova);
	}// End method...

}// End class...
